//
//  ChoosePayView.swift
//  jiuzhuangapp
//
//  Created by apple on 2021/5/18.
//

import UIKit
import FWPopupView

protocol ChoosePayDelegate:NSObjectProtocol {
    func choosePayClick(data:Dictionary<String,Any>)
}

class ChoosePayView: FWPopupView {

    @IBOutlet weak var zfbBtn: UIButton!
    @IBOutlet weak var ylBtn: UIButton!
    @IBOutlet weak var submitBtn: UIButton!
    @IBOutlet weak var priceLab: UILabel!
    var contentView:UIView!
    weak var delegate: ChoosePayDelegate?
    // 初始化时将xib中的view添加进来
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView = loadViewFromNib()
        contentView.frame = self.bounds
        contentView.backgroundColor = UIColor.white
        self.insertSubview(contentView, at: 0)
        
        self.submitBtn.layer.cornerRadius = 20
        self.submitBtn.layer.masksToBounds = true
        self.submitBtn.addTarget(self, action:#selector(submit), for: .touchUpInside)
    }
    
    // 初始化时将xib中的view添加进来
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    // 加载xib
    func loadViewFromNib() -> UIView {
        let className = type(of: self)
        let bundle = Bundle(for: className)
        let name = NSStringFromClass(className).components(separatedBy: ".").last
        let nib = UINib(nibName: name!, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        view.backgroundColor = UIColor.clear
        return view
    }
    
    func setUpData(price:String) {
        self.priceLab.text = "￥\(price)"
    }
    
    @objc func submit(){
        if self.ylBtn.isSelected == true {// 2支付宝 4微信
            self.delegate?.choosePayClick(data: ["key":"4"])
        }else{
            self.delegate?.choosePayClick(data: ["key":"2"])
        }
    }
    
    @IBAction func pay(_ sender: UIButton) {
        if sender.tag == 0 {
            self.ylBtn.isSelected = true
            self.zfbBtn.isSelected = false
        }else{
            self.ylBtn.isSelected = false
            self.zfbBtn.isSelected = true
        }
    }
    
    
    @IBAction func shutDown(_ sender: Any) {
        self.hide()
        self.hideKeyboard()
    }
    
}
